﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using PlaytimeSaveTypes;

namespace Playtime
{
    public class Joint
    {
        public string Name;
        public Joint parent;
        public float relativeRotation;
        public Vector2 relativeTranslation;
        public bool IsMirrored;

        public List<Joint> Childs;

        public Joint(string _name, float _relativeRotation, Vector2 _relativeTranslation, Joint _parent)
        {
            relativeRotation = _relativeRotation;
            relativeTranslation = _relativeTranslation;
            Name = _name;
            Childs = new List<Joint>();
            SetParent(_parent);
        }

        public Joint(JointSaveData _data)
        {
            Name = _data.data.Name;
            relativeTranslation = _data.data.relativeTranslation;
            relativeRotation = _data.data.relativeRotation;
            Childs = new List<Joint>();
        }

        public void SetParent(Joint _parent)
        {
            if (_parent != null)
            {
                _parent.Childs.Add(this);
            }
            else if(parent != null)
            {
                parent.Childs.Remove(this);
            }
            parent = _parent;
        }

        public float GetAbsoluteRotation()
        {
            float retVal;
            if (parent != null)
                retVal = parent.GetAbsoluteRotation() + relativeRotation;
            else
                retVal = relativeRotation;
            return retVal;
        }

        public Vector2 GetAbsoluteTranslation()
        {
            Vector2 retVal;
            if (parent != null)
                retVal = parent.GetAbsoluteTranslation() + Vector2.Transform(relativeTranslation, Matrix.CreateRotationZ(GetAbsoluteRotation()));
            else
                retVal = relativeTranslation;

            /*if (IsMirrored)
                retVal.X *= -1;*/
            return retVal;
        }

        public void RotateAroundTo(Vector2 _absPos)
        {
            if (parent != null)
            {
                Matrix rotationMatrix = Matrix.CreateRotationZ(parent.GetAbsoluteRotation());
                Vector2 direction = _absPos - parent.GetAbsoluteTranslation();
                direction.Normalize();
                float length = relativeTranslation.Length();
                direction *= length;
                relativeTranslation = Vector2.Transform(direction, Matrix.Invert(rotationMatrix));
            }
            else
                relativeTranslation = _absPos;
        }

        public void SetAbsoluteTranslation(Vector2 _absPos)
        {
            if (parent != null)
            {
                Matrix rotationMatrix = Matrix.CreateRotationZ(parent.GetAbsoluteRotation());
                relativeTranslation = Vector2.Transform(_absPos - parent.GetAbsoluteTranslation(), Matrix.Invert(rotationMatrix));
            }
            else
                relativeTranslation = _absPos;
        }
    }
}
